home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CHARTP10.ARJ / CATEGORY.H < prev    next >
C/C++ Source or Header  |  1992-01-26  |  3KB  |  113 lines

  1.  
  2. // Copyright 1992, David Perelman-Hall & Jamshid Afshar
  3.  
  4.  
  5. #ifndef CATEGORY_H
  6. #define CATEGORY_H
  7.  
  8. #include <iostream.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include "misc.h"
  13.  
  14. const MAX_STRING_LEN = 20;
  15. const MAX_CATEGORIES = 20;
  16.  
  17. const ALIVE=1225;
  18. const DEAD=666;
  19. class Category {
  20. friend ostream& operator << ( ostream& os, const Category& cat );
  21. private:
  22.    int alive;
  23.    char _s[MAX_STRING_LEN+1];
  24. public:
  25.    //empty constructor
  26.    Category() {
  27.       alive = ALIVE;
  28.       _s[0] = '\0';
  29.       }
  30.    //copy constructor
  31.    Category( const Category& cat ) {
  32.       alive = ALIVE;
  33.       strcpy( _s, cat._s );
  34.       strupr( _s );
  35.       }
  36.    //constructor from a passed in string
  37.    Category( const char* str ) {
  38.       alive = ALIVE;
  39.       strcpy( _s, str );
  40.       }
  41.    //ensure automatic conversion to const char pointer
  42.    operator const char* () const {
  43.       return _s;
  44.       }
  45.    //assignment operator
  46.    void operator = ( const Category& cat) {
  47.       strcpy( _s, cat._s );
  48.       }
  49.    //user define ==
  50.    bool operator == ( const Category& cat) const {
  51.       return strcmp( _s, cat._s ) == 0;
  52.       }
  53.    //user defined !=
  54.    bool operator != ( const Category& cat) const {
  55.       return !( *this == cat );
  56.       }
  57.    //destructor
  58.    ~Category();
  59.    //clear
  60.    void clear(){
  61.       _s[0] = '\0';
  62.       }
  63. };
  64.  
  65. class Category_Sequence {
  66. friend ostream& operator << ( ostream& os, const Category_Sequence& seq );
  67. friend istream& operator >> ( istream& is, Category_Sequence& seq );
  68. private:
  69.    int alive;
  70.    struct CategoryNode {
  71.       Category _category;
  72.       CategoryNode *_nextp;
  73.       CategoryNode( Category category, CategoryNode *nextp )
  74.          : _category(category), _nextp(nextp) {}
  75.       };
  76.    CategoryNode *_first_node;
  77. public:
  78.    Category_Sequence();
  79.    Category_Sequence( const Category_Sequence& );
  80.    Category_Sequence( const Category& category );
  81.    ~Category_Sequence();
  82.  
  83.    Category pop();
  84.    void operator = ( const Category_Sequence& seq);
  85.    Category_Sequence& operator += ( const Category& category);
  86.    Category_Sequence& operator += ( const Category_Sequence& seq);
  87.    void clear();
  88.  
  89.    const Category& first() const {
  90.       assert( !isEmpty() );
  91.       return _first_node->_category;
  92.       }      
  93.  
  94.    Category_Sequence rest() const;
  95.    int length() const;
  96.    bool operator == ( const Category_Sequence& seq) const;
  97.    bool isEmpty() const {
  98.       return _first_node == NULL;
  99.       }
  100. };
  101.  
  102.  
  103. inline
  104. Category_Sequence operator + ( const Category_Sequence& a, const Category& b)
  105.    {  Category_Sequence temp = a; return temp += b; }
  106.  
  107. inline
  108. Category_Sequence operator + ( const Category_Sequence& a, const Category_Sequence& b)
  109.    {  Category_Sequence temp = a; return temp += b; }
  110.  
  111. #endif
  112.  
  113.